package com.mediacloud.app.newsmodule.fragment.ugc

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.LayoutHelper
import com.alibaba.android.vlayout.layout.StaggeredGridLayoutHelper
import com.mediacloud.app.assembly.util.Utility
import com.mediacloud.app.assembly.util.loadRound
import com.mediacloud.app.assembly.views.CornerBlurView
import com.mediacloud.app.model.component.ComponentItem
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.fragment.ugc.detail.LiteAvDetailActivity
import com.mediacloud.app.newsmodule.fragment.ugc.detail.LiteAvStartMod
import com.mediacloud.app.newsmodule.utils.ComponentClickUtils
import org.json.JSONObject

class LiteAvListAdapter2(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var data: MutableList<ArticleItem> = ArrayList()
    var navigateId = ""
    var catalogId: String? = null
    var pageIndex: Int = 1
    var singleWidth: Int = 0

    init {

        singleWidth = (Utility.getScreenSize(context)[0] - 3 * context.resources.getDimensionPixelSize(R.dimen.dimen10)) / 2
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            1 -> LiteAvComponentHolder(LayoutInflater.from(context).inflate(R.layout.item_lite_av_component1, parent, false))
            else -> LiteAvListHolder(LayoutInflater.from(context).inflate(R.layout.item_lite_av_content, parent, false))
        }

    }


    fun addData(data: MutableList<ArticleItem>) {
        this.data.addAll(data)
        notifyDataSetChanged()
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is LiteAvListHolder -> {
                val d = data[position]
                val lp = holder.logo.layoutParams
                lp.height = (singleWidth / 0.886f).toInt()
                holder.logo.layoutParams = lp

                holder.logo.load(d.tipImage, true)
                val title = d.title.trimEnd()
                if (d.catalogName.isNullOrEmpty()) {
                    holder.title.text = title
                } else {
                    val hotString = "#${d.catalogName}#"
                    val spannableString = SpannableString("$hotString$title")
                    val focusTextColor = ForegroundColorSpan(Color.parseColor("#3368EA"))
                    spannableString.setSpan(focusTextColor, 0, hotString.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    holder.title.text = spannableString
                }

                holder.nickName.text = d.tipPerson
                if (d.location.isNotEmpty()) {
                    holder.location.visibility = View.VISIBLE
                    holder.location.text = d.location
                } else {
                    holder.location.visibility = View.GONE
                }
                holder.duration.text = d.duration
                holder.likes.text = "${d.favorCount_format}赞"
                holder.avatar.loadRound(d.avatar)
                if (catalogId != null) {
                    when (position) {
                        0 -> {
                            holder.rankTag.visibility = View.VISIBLE
                            holder.rankTag.setImageResource(R.drawable.liteav_rank1)
                        }
                        1 -> {
                            holder.rankTag.visibility = View.VISIBLE
                            holder.rankTag.setImageResource(R.drawable.liteav_rank2)
                        }
                        2 -> {
                            holder.rankTag.visibility = View.VISIBLE
                            holder.rankTag.setImageResource(R.drawable.liteav_rank3)
                        }
                        else -> holder.rankTag.visibility = View.GONE
                    }
                } else {
                    holder.rankTag.visibility = View.GONE
                }
                holder.itemView.setOnClickListener {
                    val temps: ArrayList<ArticleItem> = ArrayList()
                    var p = 0
                    data.forEachIndexed { index, articleItem ->
                        if (articleItem.type != 999960) {
                            temps.add(articleItem)
                        }
                        if (d == articleItem) {
                            p = temps.indexOf(articleItem)
                        }
                    }
                    val mod = LiteAvStartMod(temps, p, pageIndex, 0, navigateId, null)
                    LiteAvDetailActivity.startActivity(context, mod)

                    //    TikTokActivity.startActivity(context, data as ArrayList<ArticleItem>, position, navigateId, pageIndex, 0, true)
                }
            }
            is LiteAvComponentHolder -> {
                val d = data[position]
                val component = d.buildComponent()
                var elementItem: JSONObject? = null
                JSONObject(d.subgroup).optJSONArray("element")?.let {
                    if (it.length() > 0) {
                        elementItem = it.optJSONObject(0)
                        holder.logo.load(elementItem!!.optString("icon"), true)
                    }
                }
                holder.itemView.setOnClickListener {
                    ComponentClickUtils.OpenItemComponent(context, component, elementItem)
                }
                val ratio = component.width * 1.0f / component.height

                val lp = holder.logo.layoutParams
                if (ratio == 0f) {
                    lp.height = singleWidth
                } else {
                    lp.height = (singleWidth / ratio).toInt()
                }
                holder.logo.layoutParams = lp
            }
        }

    }

    override fun getItemViewType(position: Int): Int {
        val type = data[position].type
        return if (type == 999960) {
            1
        } else {
            0
        }

    }

    override fun getItemCount(): Int {
        return data.size
    }

    class LiteAvComponentHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val logo: CornerBlurView = itemView.findViewById(R.id.logo);
    }


    class LiteAvListHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val logo: CornerBlurView = itemView.findViewById(R.id.logo)
        val location: TextView = itemView.findViewById(R.id.location)
        val duration: TextView = itemView.findViewById(R.id.duration)
        val title: TextView = itemView.findViewById(R.id.title)
        val nickName: TextView = itemView.findViewById(R.id.nickName)
        val likes: TextView = itemView.findViewById(R.id.likes)
        val avatar: ImageView = itemView.findViewById(R.id.avatar)
        var rankTag: ImageView = itemView.findViewById(R.id.rankTag)
    }

}